﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using XinGuanERP.Attributes.system;
using XinGuanERP.Services.business.IServices;
using XinGuanERP.Utils;

namespace XinGuanERP.Controllers.system;

[DynamicApiController]
public class OssController
{
    private readonly IOssService _ossService;
    
    public OssController(IOssService ossService)
    {
        _ossService = ossService;
    }

    public ResultUtil GetOssToken()
    {
        var signMap = _ossService.getOssSign();
        if (signMap == null || signMap.Count == 0)
        {
            return ResultUtil.error("获取签名失败");
        }
        return ResultUtil.ok(signMap);
    }

    [Authorize]
    [ServiceFilter(typeof(MyActionFilterAttribute))]
    public ResultUtil UploadFile(IFormFile file)
    {
        var res = _ossService.uploadFile(file);
        if (res != null)
        {
            return ResultUtil.ok(res);
        }
        return ResultUtil.error("上传失败");        
        // if (file != null)
        // {
        //     var fileDir = "D:\\aaa";
        //     if (!Directory.Exists(fileDir))
        //     {
        //         Directory.CreateDirectory(fileDir);
        //     }
        //
        //     var projectFileName = file.FileName;
        //     var filePath = Path.Combine(fileDir, projectFileName);
        //     using (FileStream fs = System.IO.File.Create(filePath))
        //     {
        //         file.CopyTo(fs);
        //         fs.Flush();
        //     }
        //
        //     return ResultUtil.ok(null);
        // }
        //
        // return ResultUtil.error("空的");
    }

}